<?php
namespace app\command;

use app\model\OrderLogisticsModel;
use app\model\OrdersModel;
use app\model\StatisticsModel;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class UpdateLogistics extends Command
{

    protected function configure()
    {
        $this->setName('更新物流轨迹')
            ->setDescription('批量处理订单的物流信息');
    }

    protected function execute(Input $input, Output $output)
    {
        // 执行你的脚本任务代码
        $orderLogisticsModel = new OrderLogisticsModel();
        $orderModel = new OrdersModel();
        $where =[];
        $where[] = ["logistics_status","<>","300003"];
        $where[] = ["is_delete","=","0"];
        $list = $orderLogisticsModel->getList($where,1,999999);

        if($list) {
            foreach ($list as $one){
                    $response = getLogistics($one["wx_openid"],$one["waybill_id"],$one["delivery_id"]);
                    // 处理响应
                    if ($response->getStatusCode() == 200) {
                        $data = $response->getBody()->getContents();
                        $data = json_decode($data, true);
                        if(!isset($data['errcode'])){
                            if(empty($data["path_item_list"])){
                                continue;
                            }
                            $last = reset($data["path_item_list"]);
                            $orderLogisticsModel->where("logistics_id",$one["logistics_id"])->update(["logistics_status"=>$last["action_type"]]);
                            $orderLogisticsModel->delCache($one["logistics_id"]);
                            //$orderModel->updateOrderStatus($one["order_id"],3);
                        }
                    }
                }
        }

    }
}